<template>
  <div>
    <el-dialog
     title="岗位选择"
     width="70%"
     :close-on-click-modal="false"
     :append-to-body="true"
      v-dialogDrag
     :visible.sync="visible">
      <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <el-form-item prop="name">
                <el-input size="small" v-model="searchForm.name" placeholder="岗位名称" clearable></el-input>
         </el-form-item>
         <el-form-item prop="type">
                <!-- <el-input size="small" v-model="searchForm.name" placeholder="岗位类型" clearable></el-input> -->
          <el-select v-model="searchForm.type" placeholder="岗位类型"  style="width: 100%;">
                    <el-option
                      v-for="item in $dictUtils.getDictList('sys_post_type')"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                </el-select>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
      <el-row>
        <el-button-group class="pull-right">
            <el-button
              type="default"
              size="small"
              icon="el-icon-search"
              @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
            </el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
    <el-table-column
        prop="name"
        show-overflow-tooltip
        sortable="custom"
        label="岗位名称">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('sys:post:edit')" @click="edit(scope.row.id)">{{scope.row.name}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('sys:post:view')"  @click="view(scope.row.id)">{{scope.row.name}}</el-link>
              <span v-else>{{scope.row.name}}</span>
            </template>
      </el-table-column>
    <el-table-column
        prop="type"
        show-overflow-tooltip
        sortable="custom"
        label="岗位类型">
        <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("sys_post_type", scope.row.type, '-') }}
        </template>
      </el-table-column>
<!--    <el-table-column
        prop="status"
        show-overflow-tooltip
        sortable="custom"
        label="是否可用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==='1'" type="primary">
             {{ $dictUtils.getDictLabel("yes_no", scope.row.status, '-') }}
          </el-tag>
          <el-tag v-else type="danger">
             {{ $dictUtils.getDictLabel("yes_no", scope.row.status, '-') }}
          </el-tag>
        </template>
      </el-table-column> -->
<!--    <el-table-column
        prop="sort"
        show-overflow-tooltip
        sortable="custom"
        label="岗位排序">
      </el-table-column> -->
    <el-table-column
        prop="remarks"
        show-overflow-tooltip
        sortable="custom"
        label="备注信息">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="doSubmit()">确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        searchForm: {
          name: '',
          code: '',
          type: ''
        },
        id: '',
        visible: false,
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: true,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
    },
    activated () {
      this.refreshList()
    },
    props: {
    selectData: {
      type: Array,
      default: () => { return [] }
    },
    limit: {
      type: Number,
      default: 999999
    }
    },
    methods: {
      init () {
        this.searchForm.name = ''
        this.searchForm.type = ''
        this.visible = true
        this.refreshList()
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/sys/post/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },

      exportExcel () {
        this.$utils.download('/sys/post/export')
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      doSubmit () {
        if (this.limit < this.dataListSelections.length) {
          this.$message.error(`你最多只能选择${this.limit}个物料`)
          return
        }
        this.visible = false
        this.$emit('doSubmit', this.dataListSelections)
      }
    }
  }
</script>
